package edu.fudan.jwxt.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.fudan.jwxt.Exception.ServiceException;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.dao.CollegeDao;
import edu.fudan.jwxt.dao.MajorDao;
import edu.fudan.jwxt.dao.UserDao;
import edu.fudan.jwxt.entity.College;
import edu.fudan.jwxt.entity.Major;
import edu.fudan.jwxt.entity.User;
import edu.fudan.jwxt.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class MajorServiceImpl extends ServiceImpl<MajorDao, Major> implements MajorService {

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private CollegeDao collegeDao;

    @Autowired
    private UserDao userDao;



    private void addCollegeId(Major major){
        //搜索学院对应的id
        College college = collegeDao.getCollegeByName(major.getCollegeName());
        if (college == null){
            throw new ServiceException(Constants.CODE_600,"该学院不存在");
        }
        major.setCollegeId(college.getId());

    }

    private void addCollegeName(Major major){
        College college = collegeDao.selectById(major.getCollegeId());
        major.setCollegeName(college.getName());
    }

    @Override
    public List<Major> list(){
        List<Major> majors = majorDao.selectList(null);
        for(Major major : majors)
            addCollegeName(major);

        return majors;
    }

    @Override
    public Major getById(Serializable id){
        Major major = majorDao.selectById(id);
        if(major == null)
            throw new ServiceException(Constants.CODE_600,"专业不存在");
        addCollegeName(major);
        return major;
    }

    @Override
    public String getNameById(Serializable id){
        Major major=getById(id);
        return major.getMajorName();
    }

    @Override
    public boolean save(Major major){
        Major flag1 = majorDao.selectById(major.getId());
        if(flag1 != null){
            throw new ServiceException(Constants.CODE_600,"专业序号已存在");
        }

        LambdaQueryWrapper<Major> q = new LambdaQueryWrapper<>();
        q.eq(Major::getMajorName,major.getMajorName()).ne(Major::getId,major.getId());
        List<Major> flag2 = majorDao.selectList(q);

        if(flag2 != null && !flag2.isEmpty()){
            throw new ServiceException(Constants.CODE_600,"专业名称已存在");
        }




        //addCollegeId(major);
        majorDao.insert(major);
        return true;
    }

    @Override
    public boolean updateById(Major major){
        LambdaQueryWrapper<Major> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Major::getMajorName,major.getMajorName()).ne(Major::getId,major.getId());
        Major flag = majorDao.selectOne(queryWrapper);

        if(flag != null){
            throw new ServiceException(Constants.CODE_600,"专业名称已存在");
        }



        //addCollegeId(major);

        majorDao.updateById(major);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        LambdaQueryWrapper<User> q1 = new LambdaQueryWrapper<>();
        q1.eq(User::getMajorId,id);
        long userCount = userDao.selectCount(q1);
        if(userCount != 0)
            throw new ServiceException(Constants.CODE_600,"该专业下有人，请先进行清理");

        return super.removeById(id);
    }
}
